package a02_链表;

/**
 * <p>
 * a01_移除链表元素
 * </p>
 *
 * @author flyduck
 * @since 2024-06-14
 */
public class a02_移除链表元素复习3 {

    public static void main(String[] args) {
        //1,2,6,3,4,5,6
//        ListNode head = new ListNode(1);
//        head.next = new ListNode(2);
//        head.next.next = new ListNode(6);
//        head.next.next.next = new ListNode(3);
//        head.next.next.next.next = new ListNode(4);
//        head.next.next.next.next.next = new ListNode(5);
//        head.next.next.next.next.next.next = new ListNode(6);

        ListNode head = new ListNode(7);
        head.next = new ListNode(7);
        head.next.next = new ListNode(7);
        head.next.next.next = new ListNode(7);
        ListNode listNode = removeElements(head, 7);

        ListNode temp = listNode;
        while (temp != null){
            System.out.println(temp.val);
            temp = temp.next;
        }
    }

    public static ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        ListNode currentNode = dummy;
        while (currentNode != null && currentNode.next != null) {
            if(currentNode.next.val == val){
                currentNode.next = currentNode.next.next;
            }else {
                currentNode = currentNode.next;
            }
        }
        return dummy.next;
    }
}
